package com.example.swaggerdemo.config;

import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.StringVendorExtension;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;
import static springfox.documentation.builders.BuilderDefaults.nullToEmptyList;

@ConditionalOnWebApplication
@ConditionalOnClass(Docket.class)
@ConfigurationProperties(prefix = "swagger")
@Configuration
@Data
public class SwaggerConfigProperties {

	private DocumentationType documentationType = DocumentationType.OAS_30;
	private String basePackage = "com.pactera";
	private String title = "后台接口地址";
	private String version = "3.0.0";
	private String description = "后台API";
	private String contextRoot = "/";
	private boolean enableContextRoot = false;
	private Security security = new Security();

	@Data
	public static class Security {
		private Map<String, Scheme> scheme = new HashMap<>();
		private List<Map<String, String>> reference = new ArrayList<>();
	}

	@Data
	public static class Scheme {
		private String name;
		private String type;
		private String description;
		private Map<String, String> extensions = new HashMap<>();

		@SuppressWarnings("rawtypes")
		public List<VendorExtension> getVendorExtensions() {
			List<VendorExtension> vendorExtensions = new ArrayList<>();
			nullToEmptyList(extensions.entrySet()).stream().filter(input -> input.getKey().toLowerCase().startsWith("x-"))
					.collect(toList()).stream()
					.forEach(ext -> vendorExtensions.add(new StringVendorExtension(ext.getKey(), ext.getValue())));
			return vendorExtensions;
		}
	}

}
